import { MESSAGE_TIMEOUT } from "@/utils" 
import { createVNode, render } from 'vue'
import SMessage from './SMessage.vue'

const div = document.createElement('div')
// 添加到body上
document.body.appendChild(div)

// 定时器标识
let timer: any = null

const renderMessage=(vnode:any)=>{
    render(null, div)
    render(vnode, div)
    clearTimeout(timer)
    timer = setTimeout(() => {
        render(null, div)
    }, MESSAGE_TIMEOUT)
}

export default {
    error:(msg:string)=>{
        const vnode = createVNode(SMessage, { type:'error', text:msg })
        renderMessage(vnode)
    },
    warn:(msg:string)=>{
        const vnode = createVNode(SMessage, { type:'warn', text:msg })
        renderMessage(vnode)
    },
    success:(msg:string)=>{
        const vnode = createVNode(SMessage, { type:'success', text:msg })
        renderMessage(vnode)
    },
    info:(msg:string)=>{
        const vnode = createVNode(SMessage, { type:'info', text:msg })
        renderMessage(vnode)
    },
}
// export default ({ type, text,timeout=MESSAGE_TIMEOUT }: MessageProps) => {
//     const vnode = createVNode(SMessage, { type, text })
//     render(vnode, div)
//     clearTimeout(timer)
//     timer = setTimeout(() => {
//         render(null, div)
//     }, timeout)
// }